package com.ruoyi.invoice.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.core.exception.CaptchaException;
import com.ruoyi.common.core.exception.CustomException;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.invoice.domain.*;
import com.ruoyi.invoice.dto.InvoiceTemplateDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.invoice.service.IInvoiceTemplateService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 发票凭证模版Controller
 * 
 * @author ruoyi
 * @date 2025-03-05
 */
@RestController
@RequestMapping("/invoice_template")
public class InvoiceTemplateController extends BaseController
{
    @Autowired
    private IInvoiceTemplateService invoiceTemplateService;



    /**
     * 预览接口
     * @param templateId
     * @return
     */
    @GetMapping("getTemplateDetail/{templateId}")
    public AjaxResult getTemplateDetail(@PathVariable("templateId") Long templateId) {
        if (templateId == null) {
            throw new CustomException("发票凭证id不能为空");
        }
        return success(invoiceTemplateService.getTemplateDetail(templateId));
    }


    /**
     * 查询前端所需要的下拉框数据
     * @return
     */
    @GetMapping("getSelectAll")
    public AjaxResult getSelectAll(){
        return success(invoiceTemplateService.getInvoiceTemplateList());
    }


    /**
     * 查询发票凭证模版列表
     */
    @RequiresPermissions("invoice:invoice_template:list")
    @GetMapping("/list")
    public TableDataInfo list(InvoiceTemplate invoiceTemplate)
    {
        startPage();
        List<InvoiceTemplate> list = invoiceTemplateService.selectInvoiceTemplateList(invoiceTemplate);
        return getDataTable(list);
    }

    /**
     * 导出发票凭证模版列表
     */
    @RequiresPermissions("invoice:invoice_template:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, InvoiceTemplate invoiceTemplate)
    {
        List<InvoiceTemplate> list = invoiceTemplateService.selectInvoiceTemplateList(invoiceTemplate);
        ExcelUtil<InvoiceTemplate> util = new ExcelUtil<InvoiceTemplate>(InvoiceTemplate.class);
        util.exportExcel(response, list, "发票凭证模版数据");
    }

    /**
     * 获取发票凭证模版详细信息
     */
    @RequiresPermissions("invoice:invoice_template:query")
    @GetMapping(value = "/{templateId}")
    public AjaxResult getInfo(@PathVariable("templateId") Long templateId)
    {
        return success(invoiceTemplateService.selectInvoiceTemplateByTemplateId(templateId));
    }

    /**
     * 新增发票凭证模版
     */
    @RequiresPermissions("invoice:invoice_template:add")
    @PostMapping
    public AjaxResult add(@RequestBody @Validated InvoiceTemplateDTO invoiceTemplateDTO)
    {
        if (invoiceTemplateDTO.getEntries().size() < 2 || invoiceTemplateDTO.getEntries() == null) {
            throw new CaptchaException("凭证模版凭证凭证数量不能小于2");
        }

        int a = 0;
        int b = 0;
        // 遍历凭证明细，判断借贷方向是否匹配
        List<InvoiceTemplateDetail> entries = invoiceTemplateDTO.getEntries();
        for (InvoiceTemplateDetail entry : entries) {
            if (entry.getSubjectId() == null) {
                throw new CaptchaException("凭证模版凭明细不能为空");
            }
            if (entry.getDirection() == 0) {
                a++;
            } else if (entry.getDirection() == 1) {
                b++;
            }
        }

        if (a == 0 || b == 0) {
            throw new CaptchaException("凭证模版凭证方向不匹配，请检查借贷双方");
        }

        return toAjax(invoiceTemplateService.insertInvoiceTemplate(invoiceTemplateDTO));
    }

    /**
     * 修改发票凭证模版
     */
    @RequiresPermissions("invoice:invoice_template:edit")
    @PutMapping
    public AjaxResult edit(@RequestBody @Validated InvoiceTemplateDTO invoiceTemplateDTO)
    {
        if (invoiceTemplateDTO.getEntries().size() < 2 || invoiceTemplateDTO.getEntries() == null) {
            throw new CaptchaException("凭证模版凭证凭证数量不能小于2");
        }

        int a = 0;
        int b = 0;
        // 遍历凭证明细，判断借贷方向是否匹配
        List<InvoiceTemplateDetail> entries = invoiceTemplateDTO.getEntries();
        for (InvoiceTemplateDetail entry : entries) {
            if (entry.getSubjectId() == null) {
                throw new CaptchaException("凭证模版凭明细不能为空");
            }
            if (entry.getDirection() == 0) {
                a++;
            } else if (entry.getDirection() == 1) {
                b++;
            }
        }

        if (a == 0 || b == 0) {
            throw new CaptchaException("凭证模版凭证方向不匹配，请检查借贷双方");
        }

        return toAjax(invoiceTemplateService.updateInvoiceTemplate(invoiceTemplateDTO));
    }

    /**
     * 删除发票凭证模版
     */
    @RequiresPermissions("invoice:invoice_template:remove")
	@DeleteMapping("/{templateIds}")
    public AjaxResult remove(@PathVariable Long[] templateIds)
    {
        return toAjax(invoiceTemplateService.deleteInvoiceTemplateByTemplateIds(templateIds));
    }
}
